﻿using System;
using System.Collections.Specialized;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.DynamicData;
using System.Web.UI;
using System.Web.UI.WebControls;
using NotAClue.ComponentModel.DataAnnotations;
using NotAClue.Web;
using NotAClue.Web.DynamicData;

namespace DD_Extensions
{
    public partial class BooleanRadioButtons_EditField : ParentChangeNotifyingFieldTemplate //System.Web.DynamicData.FieldTemplateUserControl
    {
        private const string NullValueString = "[null]";

        protected void Page_Init(object sender, EventArgs e)
        {
            // add tool-tip
            this.SetupTip();

            // set field options
            this.SetFieldOptions();

            // add CSS from meta data
            this.ApplyCss();

            if (Mode == DataBoundControlMode.Insert && Column.IsRequired)
                RadioButtonList1.Items.Add(new ListItem("[Select one]", ""));
            else if (Mode == DataBoundControlMode.Insert || !Column.IsRequired)
                RadioButtonList1.Items.Add(new ListItem("[Not Set]", ""));

            if (Column.IsRequired)
                RadioButtonList1.CssClass += " DDRequired";

            var filterUIHint = Column.GetAttribute<UIHintAttribute>();
            if (filterUIHint != null &&
                filterUIHint.ControlParameters.Count > 0 &&
                filterUIHint.ControlParameters.ContainsKey("TrueValue") &&
                filterUIHint.ControlParameters.ContainsKey("FalseValue"))
            {
                var trueValue = filterUIHint.ControlParameters["TrueValue"].ToString();
                var falseValue = filterUIHint.ControlParameters["FalseValue"].ToString();
                RadioButtonList1.Items.Add(new ListItem(trueValue, Boolean.TrueString));
                RadioButtonList1.Items.Add(new ListItem(falseValue, Boolean.FalseString));
            }
            else
            {
                RadioButtonList1.Items.Add(new ListItem("Yes", Boolean.TrueString));
                RadioButtonList1.Items.Add(new ListItem("No", Boolean.FalseString));
            }

            SetUpValidator(RequiredFieldValidator1);
            SetUpValidator(DynamicValidator1);

            // set validators to use image as error indicator
            this.SetValidatorsToImage();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (HasEvents)
                RadioButtonList1.AutoPostBack = true;
        }

        protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            // raise event
            RaiseStatusChanged(this.RadioButtonList1.SelectedItem);

            RadioButtonList1.SetFocusHere();
        }

        protected override void OnDataBinding(EventArgs e)
        {
            base.OnDataBinding(e);

            // Set the initial value if there is one
            String initialValue = FieldValueString;
            if (!String.IsNullOrEmpty(initialValue))
                RadioButtonList1.SelectedValue = initialValue;
            else
                RadioButtonList1.SelectedValue = String.Empty;

            SetState(RadioButtonList1.SelectedItem);
        }

        protected override void ExtractValues(IOrderedDictionary dictionary)
        {
            dictionary[Column.Name] = RadioButtonList1.SelectedValue;
        }

        public override Control DataControl
        {
            get { return RadioButtonList1; }
        }
    }
}
